package com.metaera.app.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LoginVerPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.LifecycleCountDownTimer;
import com.metaera.app.utils.ToastUtils;
import com.metaera.app.utils.validated.EmptyRule;
import com.metaera.app.utils.validated.Validated;

// 手机号，账号密码
public class AccountChangeActivity extends BaseActivity {

    public static int PASSWORD = 1, MOBILE_NUMBER = 2;

    public static Intent newInstance(Context context, int type) {
        Intent intent = new Intent(context, AccountChangeActivity.class);
        intent.putExtra("EXTRA_TYPE", type);
        return intent;
    }

    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_setting_pwd;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        int extraType = getIntent().getIntExtra("EXTRA_TYPE", PASSWORD);
        if (extraType == PASSWORD) {
            renderPassword();

        } else if (extraType == MOBILE_NUMBER) {
            renderMobileNumber();
        }
    }

    // 更换手机号
    private void renderMobileNumber() {
        ActivityTitleHelper.setTitle(this, R.string.alter_mobile_number);

        // find
        EditText etVerifyCode = findViewById(R.id.et_verifyCode);
        TextView tvOldPhone = findViewById(R.id.tv_old_phone);
        findViewById(R.id.layout_2).setVisibility(View.GONE);
        findViewById(R.id.layout_3).setVisibility(View.GONE);
        Button btnOk = findViewById(R.id.btn_ok);

        btnOk.setText(R.string.next_text);

        TextView updateVerifyCode = findViewById(R.id.tv_verify_code);

        etVerifyCode.setOnFocusChangeListener((view, b) -> findViewById(R.id.layout_1).setSelected(b));

        UserDetailsResponse user = LocalConfigStore.getInstance().getUser();
        String mobile = user.getMobile();
        tvOldPhone.setText(String.format(getString(R.string.update_phone_number), mobile));

        LoginVerPresenter presenter = new LoginVerPresenter();
        presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
//            LifecycleCountDownTimer countDownTimer = new LifecycleCountDownTimer(getLifecycle(), updateVerifyCode);
            new LifecycleCountDownTimer(getLifecycle(), updateVerifyCode).start();
        }));

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        // listener
        updateVerifyCode.setOnClickListener(v -> presenter.sendSms(user.getPhone_code(), mobile));
    }

    // 修改密码
    private void renderPassword() {
        ActivityTitleHelper.setTitle(this, R.string.update_pwd);

        // find
        TextView tvOldPhone = findViewById(R.id.tv_old_phone);
        EditText etVerifyCode = findViewById(R.id.et_verifyCode);
        EditText etOnePwd = findViewById(R.id.et_one_pwd);
        EditText etConfirmPwd = findViewById(R.id.et_confirm_pwd);
        Button btnOk = findViewById(R.id.btn_ok);
        TextView tv_verify_code = findViewById(R.id.tv_verify_code);

        // init
        Validated.add(btnOk, new EmptyRule(etVerifyCode), new EmptyRule(etOnePwd), new EmptyRule(etConfirmPwd));

        UserDetailsResponse user = LocalConfigStore.getInstance().getUser();
        String mobile = user.getMobile();
        tvOldPhone.setText(String.format(getString(R.string.update_phone_number), mobile));

        etVerifyCode.setOnFocusChangeListener((view, b) -> findViewById(R.id.layout_1).setSelected(b));
        etOnePwd.setOnFocusChangeListener((view, b) -> findViewById(R.id.layout_2).setSelected(b));
        etConfirmPwd.setOnFocusChangeListener((view, b) -> findViewById(R.id.layout_3).setSelected(b));

        // network
        LoginVerPresenter pre = new LoginVerPresenter();
        pre.setModelAndView(new BasicNetworkRequestView<>(data -> finish()));

        try {
            LoginVerPresenter presenter = new LoginVerPresenter();
            presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
                new LifecycleCountDownTimer(getLifecycle(), tv_verify_code).start();
            }));

            // listener
            tv_verify_code.setOnClickListener(v -> presenter.sendSms(user.getPhone_code(), mobile));

        } catch (Exception e) {
            ToastUtils.makeText(e.getMessage());
        }

        btnOk.setOnClickListener(v -> {
            pre.resetPassword(
                    etVerifyCode.getText().toString(),
                    mobile,
                    user.getPhone_code(),
                    etOnePwd.getText().toString(),
                    etConfirmPwd.getText().toString()
            );
        });
    }

}
